package com.example.onlinemarket.model;

import com.google.gson.annotations.Expose;

import java.util.List;
import java.util.Objects;

public class OrderInfo {

    @Expose
    public List<MobileOrderItem> orderItemList;
    @Expose
    public Order order;

    @Override
    public String toString() {
        return "OrderInfo{" +
                "orderItemList=" + orderItemList +
                ", order=" + order +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof OrderInfo)) return false;
        OrderInfo info = (OrderInfo) o;
        return Objects.equals(orderItemList, info.orderItemList) &&
                Objects.equals(order, info.order);
    }

    public static void updateGoodNum(List<OrderInfo> orderInfoList, int goodId, int goodNum) {
        boolean isOk = false;
        for(OrderInfo orderInfo:orderInfoList){
            if(!isOk){
                for(MobileOrderItem orderItem:orderInfo.orderItemList){
                    if(orderItem.goodId==goodId){
                        isOk = true;
                        orderItem.goodNum = goodNum;
                        break;
                    }
                }
            }else break;
        }
    }

    public static void removeGood(List<OrderInfo> orderInfoList, int goodId){
        boolean isOk = false;
        int i,j;
        for(i=0;i<orderInfoList.size();i++){
            OrderInfo orderInfo = orderInfoList.get(i);
            if(!isOk){
                for(j=0;j<orderInfo.orderItemList.size();j++){
                    MobileOrderItem item = orderInfo.orderItemList.get(j);
                    if(item.goodId == goodId){
                        isOk = true;
                        break;
                    }
                }
                if(isOk)
                    orderInfo.orderItemList.remove(j);
            }else {
                if (orderInfoList.get(i).orderItemList.size() <= 0)
                    orderInfoList.remove(i);
                break;
            }
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(orderItemList, order);
    }
}
